package club.gskj.gulimall.order.controller;

import java.util.Arrays;
import java.util.Map;

import club.gskj.gulimall.order.entity.OrderEntity;
import club.gskj.gulimall.order.service.OrderService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.Ordered;
import org.springframework.web.bind.annotation.*;

import club.gskj.common.utils.PageUtils;
import club.gskj.common.utils.R;

/**
 * 订单
 *
 * @author 康纳-connor
 * @email 2417039231@gmail.com
 * @date 2021-03-27 16:26:19
 */
@RestController
@RequestMapping("ware/order")
public class OrderController {

    @Autowired
    private OrderService orderService;

    /***
     * 分页查询当前登陆用户的订单、订单项信息
     * @param params 查询条件
     */
    @PostMapping("/listWithItem")
    public R listOrderWithItems(@RequestBody Map<String, Object> params) {
        PageUtils page = orderService.queryPageWithItems(params);
        return R.ok().put("page", page);
    }


    /***
     *  根据订单号查询订单信息
     * @param orderSn
     * @return
     */
    @GetMapping("/status/{orderSn}")
    public R getOrderStatusByOrderSn(@PathVariable("orderSn") String orderSn) {
        OrderEntity orderEntity = orderService.getOrderStatusByOrderSn(orderSn);
        return R.ok().setData(orderEntity);
    }

    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = orderService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    public R info(@PathVariable("id") Long id) {
        OrderEntity Order = orderService.getById(id);

        return R.ok().put("Order", Order);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody OrderEntity Order) {
        orderService.save(Order);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody OrderEntity Order) {
        orderService.updateById(Order);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] ids) {
        orderService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
